var t = getApp(), a = [], s = "", e = 1;

Component({
    properties: {
        datas: {
            type: null,
            value: ""
        }
    },
    data: {
        oneClassInd: 0,
        twoClassInd: 0,
        thrClassInd: 0,
        thrMoreShow: !1,
        list: [],
        page: 1,
        isLoadMore: !0,
        total_page: 0,
        activatId: "",
        LoadMoreTest: "",
        searchScreen: {},
        carts: [],
        show2: !1,
        goodsCartsTotal: [],
        goodsInfo: {},
        scrollTop: 0,
        isclickTap: !0,
        refresher: !1,
        tapClick: !0
    },
    lifetimes: {
        attached: function() {
            for (var t = wx.getStorageSync("langIndex"), a = this.data.datas.list.list, s = 0; s < a.length; s++) {
                a[s].children.unshift({
                    id: 0,
                    name: "全部",
                    children: []
                });
            }
            this.setData({
                list: a,
                language: t.en
            }), this.data.list && (this.checkClassId(), this.updateData());
        },
        ready: function() {
            var t = this;
            this.createSelectorQuery().select(".scrollView").fields({
                node: !0,
                size: !0
            }).exec(function(a) {
                t.setData({
                    height_left: a[0].height - 100,
                    height_right: a[0].height - 95
                });
            });
        }
    },
    pageLifetimes: {
        show: function() {},
        hide: function() {},
        resize: function() {}
    },
    methods: {
        binddragstart_start: function(t) {
            var a, s = this.data.oneClassInd, e = this.data.twoClassInd, o = this.data.thrClassInd, i = this.data.list;
            i[s].children.length > 0 ? (0 == e && 0 == o && (a = this.data.activatId), console.log(i[s].children[e].children), 
            i[s].children[e].children.length > 0 ? (console.log("有三级"), 0 == o ? (console.log("有三级 ---- 三级是第一个，切换二级分类", e), 
            0 == e ? a = this.data.activatId : (e -= 1, console.log(i[s].children[e], "二级是全部"), 
            a = 0 == i[s].children[e].id ? i[s].id : i[s].children[e].children.length > 0 ? i[s].children[e].children[o].id : i[s].children[e].id)) : (o -= 1, 
            a = i[s].children[e].children[o].id)) : (console.log("没三级"), 0 == e ? (console.log("二级已经是第一个"), 
            a = this.data.activatId) : (console.log("二级不是第一个，切换二级"), e -= 1, i[s].children[e].children.length > 0 ? (console.log("上一个二级有三级分类"), 
            o = 0, this.setData({
                twoClassInd: e,
                thrClassInd: o
            }), a = i[s].children[e].children[o].id) : (console.log("上一个二级mei有三级分类"), a = 0 == i[s].children[e].id ? i[s].id : i[s].children[e].id)))) : (console.log("没有二级"), 
            a = this.data.activatId), this.setData({
                twoClassInd: e,
                thrClassInd: o,
                activatId: a,
                refresher: !1,
                page: 1,
                isLoadMore: !0,
                total_page: 0,
                searchScreen: {}
            }), this.getGoods(), console.log("binddragstart_start", t);
        },
        bindscrolltolower_end: function(t) {
            if (console.log("当前商品列表是否有加载更多", this.data.page, this.data.total_page, this.data.page < this.data.total), 
            this.data.page < this.data.total_page) return console.log(":sdasdasdasds"), void this.getMore();
        },
        init: function() {
            this.selectComponent("#sort").init(), this.setData({
                page: 1,
                isLoadMore: !0,
                total_page: 0,
                activatId: "",
                searchScreen: {}
            });
        },
        addGood: function(t) {
            var a = t.target.dataset.item || t.currentTarget.dataset.item;
            console.log(t), 1 == a.has_option ? (this.setData({
                show2: !0,
                goodsInfo: a
            }), this.initPopView()) : this.numberRight(a);
        },
        numberRight: function(a) {
            var s = this;
            console.log(a, "dddddddddd");
            var e = a.id, o = a.stock, i = a;
            if (i.vip_level_status && 1 == i.vip_level_status.status) return wx.showToast({
                title: i.vip_level_status.tips,
                icon: "none",
                duration: 2e3
            }), !1;
            if (0 != o) {
                if (!t._isTextEmpty(this.data.goodsCartsTotal[e])) {
                    if (this.data.goodsCartsTotal[e].total >= o) return console.log("数量超出范围"), void wx.showToast({
                        title: "库存不足",
                        icon: "none"
                    });
                    var d = this.data.goodsCartsTotal;
                    d = Number(d[e].total) + 1, this.data.goodsCartsTotal[e].total = d;
                }
                var n = {
                    goods_id: e,
                    total: 1
                }, r = t.getNetAddresss("member.member-cart.store");
                t._getNetWork({
                    url: r,
                    data: n,
                    success: function(a) {
                        var e = a.data;
                        1 == e.result ? (t.globalData.cart_num = t.globalData.cart_num + 1, wx.showToast({
                            title: e.msg,
                            icon: "none"
                        }), s.triggerEvent("AddCartMethod"), s.updateData()) : wx.showToast({
                            title: e.msg,
                            icon: "none"
                        });
                    }
                });
            } else wx.showToast({
                title: "库存为零",
                icon: "none"
            });
        },
        toGood: function(t) {
            var a = t.currentTarget.dataset.id;
            wx.navigateTo({
                url: "/packageA/detail_v2/detail_v2?id=" + a
            });
        },
        tapOneClass: function(t) {
            var a = t.currentTarget.dataset.ind;
            this.setData({
                oneClassInd: a,
                twoClassInd: 0,
                thrClassInd: 0
            }), this.init(), this.checkClassId();
        },
        tapTwoClass: function(t) {
            var a = t.currentTarget.dataset.ind;
            this.setData({
                twoClassInd: a,
                thrClassInd: 0
            }), this.init(), this.checkClassId();
        },
        tapThrClass: function(t) {
            var a = t.currentTarget.dataset.ind, s = t.currentTarget.dataset.id;
            this.init(), this.setData({
                thrClassInd: a,
                activatId: s,
                foodInfo: []
            }), this.getGoods();
        },
        onChange: function(t) {
            this.setData({
                activeNames: t.detail
            });
        },
        showbox: function(t) {
            console.log(t), this.setData({
                showGoodScreen: !this.data.showGoodScreen
            });
        },
        MoreBoxThrTap: function(t) {
            this.setData({
                thrMoreShow: !this.data.thrMoreShow
            });
        },
        sortOut: function(t) {
            this.setData({
                showGoodScreen: !1
            }), this.data.searchScreen.order_field = t.detail.order_field, this.data.searchScreen.order_by = t.detail.order_by, 
            this.checkClassId();
        },
        confirmGoodScreen: function(t) {
            var a = t.detail;
            this.setData({
                showGoodScreen: !1,
                searchScreen: a
            }), this.checkClassId();
        },
        cloneGoodScreen: function() {
            this.setData({
                showGoodScreen: !1
            });
        },
        checkClassId: function() {
            var t, a = this.data.oneClassInd, s = this.data.twoClassInd, e = this.data.thrClassInd, o = this.data.list;
            t = o[a].children.length > 0 ? o[a].children[s].children.length > 0 ? o[a].children[s].children[e].id : 0 == o[a].children[s].id ? o[a].id : o[a].children[s].id : o[a].id, 
            console.log(t), this.setData({
                activatId: t,
                foodInfo: []
            }), this.getGoods();
        },
        getMore: function() {
            wx.showLoading({
                title: "加载更多"
            });
            var a = this;
            if (a.data.page >= a.data.total_page) return a.setData({
                LoadMoreTest: "没有更多"
            }), void a.setData({
                isLoadMore: !1
            });
            a.setData({
                isLoadMore: !0
            });
            var s = a.data.page;
            s += 1, a.setData({
                page: s
            });
            var e, o = t.getNetAddresss("goods.category.get-goods-list-by-category-id");
            (this.data.searchScreen.top || this.data.searchScreen.mini) && (e = this.data.searchScreen.mini ? "".concat(this.data.searchScreen.mini).concat(this.data.searchScreen.top ? "," + this.data.searchScreen.top : this.data.searchScreen.top ? this.data.searchScreen.top : "") : this.data.searchScreen.top);
            var i = {
                goods_page: a.data.page,
                category_id: a.data.activatId,
                order_field: this.data.searchScreen.order_field || "",
                order_by: this.data.searchScreen.order_by || "",
                "search[brand_id]": this.data.searchScreen.brandId || "",
                "search[product_attr]": this.data.searchScreen.attrstr || "",
                "search[filtering]": this.data.searchScreen.filterId || "",
                "search[price_range]": e || ""
            };
            t._getNetWork({
                url: o,
                data: i,
                success: function(t) {
                    wx.hideLoading({
                        success: function(t) {}
                    });
                    var s = t.data;
                    if (a.setData({
                        isLoadMore: !0
                    }), 1 != s.result) {
                        var e = a.data.page;
                        return e -= 1, void a.setData({
                            page: e,
                            isLoadMore: !1
                        });
                    }
                    var o = a.data.foodInfo;
                    o = o.concat(s.data.data), a.setData({
                        foodInfo: o
                    });
                },
                fail: function(t) {
                    wx.hideLoading({
                        success: function(t) {}
                    }), console.log(t);
                }
            });
        },
        getGoods: function() {
            var a = this;
            if (console.log(this.data.scrollTop), this.data.isclickTap) {
                this.data.isclickTap = !1;
                var s, e = this, o = t.getNetAddresss("goods.category.get-goods-list-by-category-id"), i = this.data.oneClassInd, d = this.data.twoClassInd;
                if (this.data.list[i].children[d].children.length > 0) this.createSelectorQuery().select(".scrollView").fields({
                    node: !0,
                    size: !0
                }).exec(function(t) {
                    a.setData({
                        height_right: t[0].height - 200
                    }), console.log(a.data.height_right, "wwwwwwwwwwww");
                }); else this.createSelectorQuery().select(".scrollView").fields({
                    node: !0,
                    size: !0
                }).exec(function(t) {
                    a.setData({
                        height_right: t[0].height - 95
                    }), console.log(a.data.height_right, "wwwwwwwwwwww");
                });
                (this.data.searchScreen.top || this.data.searchScreen.mini) && (s = this.data.searchScreen.mini ? "".concat(this.data.searchScreen.mini).concat(this.data.searchScreen.top ? "," + this.data.searchScreen.top : this.data.searchScreen.top ? this.data.searchScreen.top : "") : this.data.searchScreen.top);
                var n = {
                    goods_page: e.data.page,
                    category_id: e.data.activatId,
                    order_field: this.data.searchScreen.order_field || "",
                    order_by: this.data.searchScreen.order_by || "",
                    "search[brand_id]": this.data.searchScreen.brandId || "",
                    "search[product_attr]": this.data.searchScreen.attrstr || "",
                    "search[filtering]": this.data.searchScreen.filterId || "",
                    "search[price_range]": s || ""
                };
                wx.showLoading({
                    title: "商品加载中"
                }), t._getNetWork({
                    url: o,
                    data: n,
                    success: function(t) {
                        wx.hideLoading();
                        var s = t.data;
                        e.setData({
                            isLoadMore: !0
                        }), setTimeout(function() {
                            a.data.isclickTap = !0;
                        }, 500), 1 == s.result ? (a.setData({
                            page: 1,
                            isLoadMore: !0,
                            total_page: s.data.last_page,
                            foodInfo: s.data.data,
                            scrollTop: 0
                        }), 1 == s.data.last_page ? a.setData({
                            LoadMoreTest: "没有更多"
                        }) : a.setData({
                            LoadMoreTest: "加载更多"
                        }), a.data.total_page || a.setData({
                            total_page: 0
                        })) : (wx.showToast({
                            title: s.msg,
                            icon: "none"
                        }), console.log(s.msg));
                    },
                    fail: function(t) {
                        wx.hideLoading(), this.data.isclickTap = !0, console.log(t);
                    }
                });
            }
        },
        submitAction: function() {
            var e = this, o = this;
            if (a.length < this.data.goodsInfo.has_many_specs.length) return this.setData({
                show2: !1
            }), void wx.showToast({
                title: this.data.goodsDescription,
                icon: "none"
            });
            var i = t.getNetAddresss("member.member-cart.store"), d = {
                goods_id: this.data.goodsInfo.id,
                total: 1,
                option_id: s
            };
            t._getNetWork({
                url: i,
                data: d,
                success: function(a) {
                    var s = a.data;
                    1 == s.result ? (t.globalData.cart_num = s.data.cart_num, wx.showToast({
                        title: s.msg,
                        icon: "none"
                    }), e.triggerEvent("AddCartMethod"), o.updateData(), o.setData({
                        popNum: o.data.popNum + 1
                    })) : wx.showToast({
                        title: s.msg,
                        icon: "none"
                    });
                },
                fail: function(t) {
                    console.log(t);
                }
            });
        },
        updateData: function() {
            var a = this, s = t.getNetAddresss("member.member-cart.index");
            t._getNetWork({
                url: s,
                success: function(t) {
                    var s = t.data;
                    1 == s.result ? (console.log(s.data), a.getCart(s.data)) : wx.showToast({
                        title: s.msg,
                        icon: "none"
                    });
                }
            });
        },
        destroyData: function(a) {
            var s = this, e = t.getNetAddresss("member.member-cart.destroy"), o = {
                ids: a
            };
            t._getNetWork({
                url: e,
                data: o,
                success: function(a) {
                    var e = a.data;
                    s.data.tapClick = !0, 1 == e.result ? (wx.showToast({
                        title: e.msg,
                        icon: "none"
                    }), t.globalData.cart_num = t.globalData.cart_num - 1, s.triggerEvent("AddCartMethod"), 
                    s.updateData(), s.setData({
                        show2: !1
                    })) : wx.showToast({
                        title: e.msg,
                        icon: "none"
                    });
                }
            });
        },
        changeAddCount: function(a) {
            var s = this;
            if (this.data.tapClick) {
                this.data.tapClick = !1;
                var e = a.target.dataset.id || a.currentTarget.dataset.id, o = a.target.dataset.num || a.currentTarget.dataset.num, i = a.target.dataset.i || a.currentTarget.dataset.i;
                if (i = Number(i), o <= 0) this.destroyData(e); else {
                    var d = {
                        id: e,
                        num: o
                    }, n = t.getNetAddresss("member.member-cart.updateNumV2");
                    t._getNetWork({
                        url: n,
                        data: d,
                        success: function(a) {
                            setTimeout(function() {
                                s.data.tapClick = !0;
                            }, 1e3);
                            var e = a.data;
                            if (1 == e.result) {
                                if (wx.showToast({
                                    title: e.msg,
                                    icon: "none"
                                }), !t._isTextEmpty(i)) {
                                    var o = s.data.popNum;
                                    switch (i) {
                                      case -1:
                                        o -= 1, s.setData({
                                            popNum: o
                                        });
                                        break;

                                      case 1:
                                        o += 1, s.setData({
                                            popNum: o
                                        });
                                        break;

                                      default:
                                        console.log("error");
                                    }
                                }
                                s.triggerEvent("AddCartMethod"), s.updateData();
                            } else wx.showToast({
                                title: e.msg,
                                icon: "none"
                            }), s.triggerEvent("AddCartMethod"), s.updateData();
                        },
                        fail: function(t) {
                            s.data.tapClick = !0;
                        }
                    });
                }
            }
        },
        getCart: function(t) {
            var a = this;
            this.setData({
                carts: t,
                goodsCarts: [],
                cartIDs: []
            });
            var e = {};
            this.data.carts.forEach(function(o) {
                e[o.goods_id] = o, t && t.id == o.goods_id && (t.buyNum = o.total), o.goods_id == a.data.goodsInfo.id && o.option_id == s && a.setData({
                    popNum: o.total,
                    popCard: o
                }), a.data.cartIDs.push(o.id);
            }), this.setData({
                goodsCarts: e
            });
            var o = this.calculateTotal(this.data.carts);
            this.setData({
                goodsCartsTotal: o
            });
        },
        calculateTotal: function(t) {
            var a = [], s = {}, e = {};
            for (var o in t) {
                var i = t[o].goods_id;
                s[i] ? s[i].total = s[i].total + t[o].total : (s[i] = {}, s[i].goods_id = t[o].goods_id, 
                s[i].total = t[o].total);
            }
            for (var d in s) a.push(s[d]);
            return a.forEach(function(t) {
                e[t.goods_id] = t;
            }), e;
        },
        initPopView: function() {
            if (s = "", a = [], this.setData({
                specid: "",
                popNum: 0
            }), 1 == this.data.goodsInfo.has_option) {
                if (this.setData({
                    popTitle: this.data.goodsInfo.title,
                    popThumb: this.data.goodsInfo.thumb,
                    popStock: this.data.goodsInfo.stock,
                    popCard: {}
                }), !this.data.specid.length) {
                    var t = this.data.goodsInfo.min_price + "-" + this.data.goodsInfo.max_price;
                    this.data.goodsInfo.point_goods && (t = this.data.goodsInfo.point_goods.min_point + "-" + this.data.goodsInfo.point_goods.max_point), 
                    this.setData({
                        popPrice: t
                    });
                }
                var e;
                this.setData({
                    goodsDescription: "请选择"
                });
                for (var o = 0; o < this.data.goodsInfo.has_many_specs.length; o++) e = this.data.goodsDescription, 
                e += "" + this.data.goodsInfo.has_many_specs[o].title;
                if (this.setData({
                    goodsDescription: e
                }), !this.data.optionsId) for (var i = 0; i < this.data.goodsInfo.has_many_specs.length; i++) if (this.data.goodsInfo.goods_option_ids && this.data.goodsInfo.goods_option_ids.length >= 0) for (var d = 0; d < this.data.goodsInfo.has_many_specs[i].has_many_specs_item.length; d++) this.data.goodsInfo.has_many_specs[i].has_many_specs_item[d].id == Number(this.data.goodsInfo.goods_option_ids[i]) && this.selectSpecs(this.data.goodsInfo.has_many_specs[i].has_many_specs_item[d], "first", i); else this.selectSpecs(this.data.goodsInfo.has_many_specs[i].has_many_specs_item[0], "first", i);
            } else this.setData({
                goodsDescription: "",
                optionsMaxCount: this.data.goodsInfo.stock
            });
        },
        selectSpecs: function(t, a, s) {
            var e = {}, o = 0;
            "first" == a ? (e = t, o = t.id) : (e = t.target.dataset.item || t.currentTarget.dataset.item, 
            o = t.target.dataset.id || t.currentTarget.dataset.id), this.manageSpecs(e), this.setGoodsSpecs(e), 
            this.setGoodsSpecsChangeInfo(o), this.getMaxCount();
        },
        manageSpecs: function(t) {
            var s = {};
            if (s.id = t.id, s.specid = t.specid, s.title = t.title, a.length > 0) {
                for (var e = 0; e < a.length; e++) a[e].specid == s.specid && a.splice(e, 1);
                a.push(s);
            } else a.push(s);
            if (a.length == this.data.goodsInfo.has_many_specs.length) {
                for (var o = [], i = 0; i < this.data.goodsInfo.has_many_specs.length; i++) for (var d = 0; d < a.length; d++) if (this.data.goodsInfo.has_many_specs[i].id == a[d].specid) {
                    o.push(a[d]);
                    break;
                }
                a = o;
            }
            this.setGoodsDescription();
        },
        setGoodsDescription: function() {
            var t = "";
            if (a.length == this.data.goodsInfo.has_many_specs.length) {
                t = "已选择 ";
                for (var s = 0; s < a.length; s++) t += a[s].title + " ";
                this.setData({
                    goodsDescription: t
                });
            } else {
                t = "请选择 ";
                for (var e = 0; e < this.data.goodsInfo.has_many_specs.length; e++) for (var o = 0; o < a.length; o++) if (this.data.goodsInfo.has_many_specs[e].id != a[o].specid) {
                    t += this.data.goodsInfo.has_many_specs[e].title + " ";
                    break;
                }
                this.setData({
                    goodsDescription: t
                });
            }
        },
        getMaxCount: function() {
            a.length == this.data.goodsInfo.has_many_specs.length && (0 == e && this.setData({
                popNum: 0
            }), this.data.popNum > e && this.setData({
                popNum: e
            }));
        },
        setGoodsSpecs: function(t) {
            for (var a = 0; a < this.data.goodsInfo.has_many_specs.length; a++) t.specid != this.data.goodsInfo.has_many_specs[a].id && this.setGoodsSpecsStatus(this.data.goodsInfo.has_many_specs[a].has_many_specs_item, t.id);
        },
        setGoodsSpecsStatus: function(t, a) {
            for (var s = [], e = 0; e < this.data.goodsInfo.has_many_options.length; e++) for (var o = this.data.goodsInfo.has_many_options[e].specs.split("_"), i = 0; i < o.length; i++) o[i] == a && s.push(this.data.goodsInfo.has_many_options[e]);
            for (var d = 0; d < s.length; d++) for (var n = s[d].specs.split("_"), r = 0; r < n.length; r++) if (n[r] != a && 0 == s[d].stock) for (var h = 0; h < t.length; h++) n[r] == t[h].id && (t[h].c = !0); else if (n[r] != a && s[d].stock > 0) for (var c = 0; c < t.length; c++) n[r] == t[c].id && (t[c].c = !1);
        },
        setGoodsSpecsBySort: function(t) {
            var a = t.split("_");
            a.sort(function(t, a) {
                return t - a;
            });
            for (var s = "", e = 0; e < a.length; e++) s += a[e] + "_";
            return s = s.substring(0, s.length - 1);
        },
        setGoodsSpecsChangeInfo: function(o) {
            var i = "";
            if (a.sort(function(t, a) {
                return t.id - a.id;
            }), a.length === this.data.goodsInfo.has_many_specs.length) {
                for (var d = "", n = 0; n < a.length; n++) d += a[n].id + "_";
                d = d.substring(0, d.length - 1), this.setData({
                    specid: d
                });
                for (var r = 0; r < this.data.goodsInfo.has_many_options.length; r++) if (d === this.setGoodsSpecsBySort(this.data.goodsInfo.has_many_options[r].specs)) {
                    i = this.data.goodsInfo.point_goods ? this.data.goodsInfo.point_goods.point_option[r].goods_point : this.data.goodsInfo.has_many_options[r].product_price, 
                    this.setData({
                        popPrice: i
                    });
                    var h = t._isTextEmpty(this.data.goodsInfo.has_many_options[r].thumb) ? this.data.goodsInfo.thumb : this.data.goodsInfo.has_many_options[r].thumb;
                    this.setData({
                        popThumb: h,
                        popStock: this.data.goodsInfo.has_many_options[r].stock
                    }), s = this.data.goodsInfo.has_many_options[r].id, (e = this.data.goodsInfo.has_many_options[r].stock) > 0 && this.setData({
                        popNum: 0
                    }), this.data.isMultiSpecs && this.getMemberPrice(s);
                    break;
                }
                for (var c = 0; c < this.data.carts.length; c++) {
                    if (this.data.carts[c].goods_id == this.data.goodsInfo.id && this.data.carts[c].option_id == s) {
                        this.setData({
                            popNum: this.data.carts[c].total,
                            popCard: this.data.carts[c]
                        });
                        break;
                    }
                    this.setData({
                        popNum: 0,
                        popCard: {}
                    });
                }
            } else {
                for (var l = "", g = 0; g < a.length; g++) l += a[g].id + "_";
                this.setData({
                    specid: l
                });
            }
        },
        getMemberPrice: function(a) {
            var s = this;
            if (a) {
                var e = 0;
                this.setData({
                    specsVIPPrc: e
                });
                var o = t.getNetAddresss("goods.other-info.option-vpi-price");
                o += "&option_id=" + a, t._getNetWork({
                    url: o,
                    success: function(t) {
                        var a = t.data;
                        e = 1 == a.result && a.data.vip_price || null, s.setData({
                            specsVIPPrc: e
                        });
                    },
                    fail: function(t) {
                        e = null, this.setData({
                            specsVIPPrc: e
                        });
                    }
                });
            }
        },
        close: function() {
            this.setData({
                show2: !1
            });
        }
    }
});